<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="dubbo,">










<meta name="description" content="Dubbo可以仅依赖jdk运行，但是同时支持了Spring和API两种形式的使用方法。这里仅介绍在dubbo是如何在Spring的框架下对应用进行透明化接入。">
<meta name="keywords" content="dubbo">
<meta property="og:type" content="article">
<meta property="og:title" content="dubbo启动">
<meta property="og:url" content="https://arrycoder.gitee.io/dubbo/Dubbo启动/index.html">
<meta property="og:site_name" content="arry&#39;s blog">
<meta property="og:description" content="Dubbo可以仅依赖jdk运行，但是同时支持了Spring和API两种形式的使用方法。这里仅介绍在dubbo是如何在Spring的框架下对应用进行透明化接入。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-07-02T09:07:58.175Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="dubbo启动">
<meta name="twitter:description" content="Dubbo可以仅依赖jdk运行，但是同时支持了Spring和API两种形式的使用方法。这里仅介绍在dubbo是如何在Spring的框架下对应用进行透明化接入。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://arrycoder.gitee.io/dubbo/Dubbo启动/">





  <title>dubbo启动 | arry's blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?97f9adfda8d04eb91dd054a98264d191";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">arry's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Tags
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/dubbo/Dubbo启动/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">dubbo启动</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-09T14:32:42+08:00">
                2019-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/dubbo/" itemprop="url" rel="index">
                    <span itemprop="name">dubbo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Dubbo可以仅依赖jdk运行，但是同时支持了Spring和API两种形式的使用方法。这里仅介绍在dubbo是如何在Spring的框架下对应用进行透明化接入。</p>
<a id="more"></a>

<p>Dubbo自定义了一套xml标准(<a href="http://dubbo.apache.org/schema/dubbo/dubbo.xsd" target="_blank" rel="noopener">schema</a>)，以及标签解析器。依赖这两个东西，就可以无缝的接入Spring中。扫描到dubbo标签的时候，就可以使用指定的BeanDefinitionParser将标签转换为Beandefinition，然后注册到Spring的上下文之中。我们可以看到，所有的dubbo标签，都依赖一个通用的 <strong>DubboBeanDefinitionParser</strong>。</p>
<p>最后一个<strong>AnnotationBeanDefinitionParser</strong>是对注解配置的支持。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DubboNamespaceHandler</span> <span class="keyword">extends</span> <span class="title">NamespaceHandlerSupport</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        Version.checkDuplicate(DubboNamespaceHandler.class);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"application"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(ApplicationConfig.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"module"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(ModuleConfig.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"registry"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(RegistryConfig.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"monitor"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(MonitorConfig.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"provider"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(ProviderConfig.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"consumer"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(ConsumerConfig.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"protocol"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(ProtocolConfig.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"service"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(ServiceBean.class, <span class="keyword">true</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"reference"</span>, <span class="keyword">new</span> DubboBeanDefinitionParser(ReferenceBean.class, <span class="keyword">false</span>));</span><br><span class="line">        registerBeanDefinitionParser(<span class="string">"annotation"</span>, <span class="keyword">new</span> AnnotationBeanDefinitionParser());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<hr>
<p>##DubboBeanDefinitionParser</p>
<p>在这个标签解析器里，会将xml的配置转换成beandefinition。</p>
<p>关注一下<strong>serviceBean</strong>和<strong>referenceBean</strong>，当我们配置consumer/provider的时候，通常是这样配置的</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//import</span><br><span class="line"><span class="tag">&lt;<span class="name">dubbo:service</span> <span class="attr">interface</span>=<span class="string">"com.alibaba.dubbo.examples.version.api.VersionService"</span> <span class="attr">version</span>=<span class="string">"2.0.0"</span>  <span class="attr">ref</span>=<span class="string">"versionService"</span>/&gt;</span></span><br><span class="line">// export</span><br><span class="line"><span class="tag">&lt;<span class="name">dubbo:reference</span> <span class="attr">id</span>=<span class="string">"demoService"</span> <span class="attr">interface</span>=<span class="string">"com.alibaba.dubbo.rpc.gen.thrift.Demo$Iface"</span> <span class="attr">timeout</span>=<span class="string">"1000000"</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<p>在引入服务的时候，会将该标签解析为一个<strong>ReferenceBean</strong>，referenceBean实现了FactoryBean接口，在获取Bean的时候会调用getObject方法，得到Dubbo生成的代理类，用来提供给消费者调用。</p>
<p>导出服务的时候，标签解析成<strong>ServiceBean</strong>，ServiceBean实现类<strong>ApplicationListener</strong>接口，监听Spring上下文事件，所以导出的逻辑会在<strong>onApplicationEvent</strong>方法中触发，包括打开端口，生成代理类，更新服务目录等工作。</p>
<h3 id="代码解析"><a href="#代码解析" class="headerlink" title="代码解析"></a>代码解析</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line">   <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> BeanDefinition <span class="title">parse</span><span class="params">(Element element, ParserContext parserContext, Class&lt;?&gt; beanClass, <span class="keyword">boolean</span> required)</span> </span>&#123;</span><br><span class="line">       RootBeanDefinition beanDefinition = <span class="keyword">new</span> RootBeanDefinition();</span><br><span class="line">       beanDefinition.setBeanClass(beanClass);</span><br><span class="line">       beanDefinition.setLazyInit(<span class="keyword">false</span>);</span><br><span class="line"></span><br><span class="line">       String id = element.getAttribute(<span class="string">"id"</span>);</span><br><span class="line">        <span class="comment">// 通用逻辑，获取bean的id。</span></span><br><span class="line">       <span class="comment">// 1-&gt;获取name属性</span></span><br><span class="line">       <span class="comment">// 2-&gt;不满足则获取接口/类名称</span></span><br><span class="line">       <span class="keyword">if</span> ((id == <span class="keyword">null</span> || id.length() == <span class="number">0</span>) &amp;&amp; required) &#123;</span><br><span class="line">           String generatedBeanName = element.getAttribute(<span class="string">"name"</span>);</span><br><span class="line">           <span class="keyword">if</span> (generatedBeanName == <span class="keyword">null</span> || generatedBeanName.length() == <span class="number">0</span>) &#123;</span><br><span class="line">               <span class="keyword">if</span> (ProtocolConfig.class.equals(beanClass)) &#123;</span><br><span class="line">                   generatedBeanName = <span class="string">"dubbo"</span>;</span><br><span class="line">               &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                   generatedBeanName = element.getAttribute(<span class="string">"interface"</span>);</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">if</span> (generatedBeanName == <span class="keyword">null</span> || generatedBeanName.length() == <span class="number">0</span>) &#123;</span><br><span class="line">               generatedBeanName = beanClass.getName();</span><br><span class="line">           &#125;</span><br><span class="line">           id = generatedBeanName;</span><br><span class="line">           <span class="keyword">int</span> counter = <span class="number">2</span>;</span><br><span class="line">           <span class="keyword">while</span> (parserContext.getRegistry().containsBeanDefinition(id)) &#123;</span><br><span class="line">               id = generatedBeanName + (counter++);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="comment">// 检查bean id是否重复，并注册</span></span><br><span class="line">       <span class="keyword">if</span> (id != <span class="keyword">null</span> &amp;&amp; id.length() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">           <span class="keyword">if</span> (parserContext.getRegistry().containsBeanDefinition(id)) &#123;</span><br><span class="line">               <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"Duplicate spring bean id "</span> + id);</span><br><span class="line">           &#125;</span><br><span class="line">           parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);</span><br><span class="line">           beanDefinition.getPropertyValues().addPropertyValue(<span class="string">"id"</span>, id);</span><br><span class="line">       &#125;</span><br><span class="line">     </span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">        * 以下是缺省配置的读取</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">       <span class="comment">// dubbo:protocol 服务提供者的缺省配置，支持配置多个</span></span><br><span class="line">       <span class="keyword">if</span> (ProtocolConfig.class.equals(beanClass)) &#123;</span><br><span class="line">           <span class="keyword">for</span> (String name : parserContext.getRegistry().getBeanDefinitionNames()) &#123;</span><br><span class="line">               BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);</span><br><span class="line">               PropertyValue property = definition.getPropertyValues().getPropertyValue(<span class="string">"protocol"</span>);</span><br><span class="line">               <span class="keyword">if</span> (property != <span class="keyword">null</span>) &#123;</span><br><span class="line">                   Object value = property.getValue();</span><br><span class="line">                   <span class="keyword">if</span> (value <span class="keyword">instanceof</span> ProtocolConfig &amp;&amp; id.equals(((ProtocolConfig) value).getName())) &#123;</span><br><span class="line">                       definition.getPropertyValues().addPropertyValue(<span class="string">"protocol"</span>, <span class="keyword">new</span> RuntimeBeanReference(id));</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">     <span class="comment">// 提供者的默认实现类</span></span><br><span class="line">       <span class="keyword">else</span> <span class="keyword">if</span> (ServiceBean.class.equals(beanClass)) &#123;</span><br><span class="line">           String className = element.getAttribute(<span class="string">"class"</span>);</span><br><span class="line">           <span class="keyword">if</span> (className != <span class="keyword">null</span> &amp;&amp; className.length() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">               RootBeanDefinition classDefinition = <span class="keyword">new</span> RootBeanDefinition();</span><br><span class="line">               classDefinition.setBeanClass(ReflectUtils.forName(className));</span><br><span class="line">               classDefinition.setLazyInit(<span class="keyword">false</span>);</span><br><span class="line">               parseProperties(element.getChildNodes(), classDefinition);</span><br><span class="line">               beanDefinition.getPropertyValues().addPropertyValue(<span class="string">"ref"</span>, <span class="keyword">new</span> BeanDefinitionHolder(classDefinition, id + <span class="string">"Impl"</span>));</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="comment">//dubbo:provider,也是为service和protocol提供缺省配置</span></span><br><span class="line">       <span class="keyword">else</span> <span class="keyword">if</span> (ProviderConfig.class.equals(beanClass)) &#123;</span><br><span class="line">           parseNested(element, parserContext, ServiceBean.class, <span class="keyword">true</span>, <span class="string">"service"</span>, <span class="string">"provider"</span>, id, beanDefinition);</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="comment">//dubbo:consumer 为reference提供缺省配置</span></span><br><span class="line">       <span class="keyword">else</span> <span class="keyword">if</span> (ConsumerConfig.class.equals(beanClass)) &#123;</span><br><span class="line">           parseNested(element, parserContext, ReferenceBean.class, <span class="keyword">false</span>, <span class="string">"reference"</span>, <span class="string">"consumer"</span>, id, beanDefinition);</span><br><span class="line">       &#125;</span><br><span class="line">     </span><br><span class="line">       Set&lt;String&gt; props = <span class="keyword">new</span> HashSet&lt;String&gt;();</span><br><span class="line">       ManagedMap parameters = <span class="keyword">null</span>;</span><br><span class="line">       <span class="comment">// 遍历setter方法，读取标签配置属性，add到beanDefinition里，用于后续实例化bean</span></span><br><span class="line">       <span class="keyword">for</span> (Method setter : beanClass.getMethods()) &#123;</span><br><span class="line">           String name = setter.getName();</span><br><span class="line">           <span class="keyword">if</span> (name.length() &gt; <span class="number">3</span> &amp;&amp; name.startsWith(<span class="string">"set"</span>)</span><br><span class="line">                   &amp;&amp; Modifier.isPublic(setter.getModifiers())</span><br><span class="line">                   &amp;&amp; setter.getParameterTypes().length == <span class="number">1</span>) &#123;</span><br><span class="line">               Class&lt;?&gt; type = setter.getParameterTypes()[<span class="number">0</span>];</span><br><span class="line">               String propertyName = name.substring(<span class="number">3</span>, <span class="number">4</span>).toLowerCase() + name.substring(<span class="number">4</span>);</span><br><span class="line">               String property = StringUtils.camelToSplitName(propertyName, <span class="string">"-"</span>);</span><br><span class="line">               props.add(property);</span><br><span class="line">               Method getter = <span class="keyword">null</span>;</span><br><span class="line">               <span class="keyword">try</span> &#123;</span><br><span class="line">                   getter = beanClass.getMethod(<span class="string">"get"</span> + name.substring(<span class="number">3</span>), <span class="keyword">new</span> Class&lt;?&gt;[<span class="number">0</span>]);</span><br><span class="line">               &#125; <span class="keyword">catch</span> (NoSuchMethodException e) &#123;</span><br><span class="line">                   <span class="keyword">try</span> &#123;</span><br><span class="line">                       getter = beanClass.getMethod(<span class="string">"is"</span> + name.substring(<span class="number">3</span>), <span class="keyword">new</span> Class&lt;?&gt;[<span class="number">0</span>]);</span><br><span class="line">                   &#125; <span class="keyword">catch</span> (NoSuchMethodException e2) &#123;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">               <span class="keyword">if</span> (getter == <span class="keyword">null</span></span><br><span class="line">                       || !Modifier.isPublic(getter.getModifiers())</span><br><span class="line">                       || !type.equals(getter.getReturnType())) &#123;</span><br><span class="line">                   <span class="keyword">continue</span>;</span><br><span class="line">               &#125;</span><br><span class="line">             <span class="comment">// 对子节点的解析</span></span><br><span class="line">               <span class="keyword">if</span> (<span class="string">"parameters"</span>.equals(property)) &#123;</span><br><span class="line">                   parameters = parseParameters(element.getChildNodes(), beanDefinition);</span><br><span class="line">               &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"methods"</span>.equals(property)) &#123;</span><br><span class="line">                 <span class="comment">// serviceBean/referenceBean的method配置解析</span></span><br><span class="line">                   parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);</span><br><span class="line">               &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"arguments"</span>.equals(property)) &#123;</span><br><span class="line">                   parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);</span><br><span class="line">               &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                   String value = element.getAttribute(property);</span><br><span class="line">                   <span class="keyword">if</span> (value != <span class="keyword">null</span>) &#123;</span><br><span class="line">                       value = value.trim();</span><br><span class="line">                       <span class="keyword">if</span> (value.length() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                           <span class="keyword">if</span> (<span class="string">"registry"</span>.equals(property) &amp;&amp; RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) &#123;</span><br><span class="line">                               RegistryConfig registryConfig = <span class="keyword">new</span> RegistryConfig();</span><br><span class="line">                               registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);</span><br><span class="line">                               beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);</span><br><span class="line">                           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"registry"</span>.equals(property) &amp;&amp; value.indexOf(<span class="string">','</span>) != -<span class="number">1</span>) &#123;</span><br><span class="line">                               parseMultiRef(<span class="string">"registries"</span>, value, beanDefinition, parserContext);</span><br><span class="line">                           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"provider"</span>.equals(property) &amp;&amp; value.indexOf(<span class="string">','</span>) != -<span class="number">1</span>) &#123;</span><br><span class="line">                               parseMultiRef(<span class="string">"providers"</span>, value, beanDefinition, parserContext);</span><br><span class="line">                           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"protocol"</span>.equals(property) &amp;&amp; value.indexOf(<span class="string">','</span>) != -<span class="number">1</span>) &#123;</span><br><span class="line">                               parseMultiRef(<span class="string">"protocols"</span>, value, beanDefinition, parserContext);</span><br><span class="line">                           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                               Object reference;</span><br><span class="line">                               <span class="keyword">if</span> (isPrimitive(type)) &#123;</span><br><span class="line">                                   <span class="keyword">if</span> (<span class="string">"async"</span>.equals(property) &amp;&amp; <span class="string">"false"</span>.equals(value)</span><br><span class="line">                                           || <span class="string">"timeout"</span>.equals(property) &amp;&amp; <span class="string">"0"</span>.equals(value)</span><br><span class="line">                                           || <span class="string">"delay"</span>.equals(property) &amp;&amp; <span class="string">"0"</span>.equals(value)</span><br><span class="line">                                           || <span class="string">"version"</span>.equals(property) &amp;&amp; <span class="string">"0.0.0"</span>.equals(value)</span><br><span class="line">                                           || <span class="string">"stat"</span>.equals(property) &amp;&amp; <span class="string">"-1"</span>.equals(value)</span><br><span class="line">                                           || <span class="string">"reliable"</span>.equals(property) &amp;&amp; <span class="string">"false"</span>.equals(value)) &#123;</span><br><span class="line">                                       <span class="comment">// backward compatibility for the default value in old version's xsd</span></span><br><span class="line">                                       <span class="comment">// 默认值的向后兼容性</span></span><br><span class="line">                                       value = <span class="keyword">null</span>;</span><br><span class="line">                                   &#125;</span><br><span class="line">                                   reference = value;</span><br><span class="line">                               &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"protocol"</span>.equals(property)</span><br><span class="line">                                       &amp;&amp; ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)</span><br><span class="line">                                       &amp;&amp; (!parserContext.getRegistry().containsBeanDefinition(value)</span><br><span class="line">                                       || !ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) &#123;</span><br><span class="line">                                   <span class="keyword">if</span> (<span class="string">"dubbo:provider"</span>.equals(element.getTagName())) &#123;</span><br><span class="line">                                       logger.warn(<span class="string">"Recommended replace &lt;dubbo:provider protocol=\""</span> + value + <span class="string">"\" ... /&gt; to &lt;dubbo:protocol name=\""</span> + value + <span class="string">"\" ... /&gt;"</span>);</span><br><span class="line">                                   &#125;</span><br><span class="line">                                   <span class="comment">// backward compatibility</span></span><br><span class="line">                                   ProtocolConfig protocol = <span class="keyword">new</span> ProtocolConfig();</span><br><span class="line">                                   protocol.setName(value);</span><br><span class="line">                                   reference = protocol;</span><br><span class="line">                               &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"onreturn"</span>.equals(property)) &#123;</span><br><span class="line">                                   <span class="keyword">int</span> index = value.lastIndexOf(<span class="string">"."</span>);</span><br><span class="line">                                   String returnRef = value.substring(<span class="number">0</span>, index);</span><br><span class="line">                                   String returnMethod = value.substring(index + <span class="number">1</span>);</span><br><span class="line">                                   reference = <span class="keyword">new</span> RuntimeBeanReference(returnRef);</span><br><span class="line">                                   beanDefinition.getPropertyValues().addPropertyValue(<span class="string">"onreturnMethod"</span>, returnMethod);</span><br><span class="line">                               &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"onthrow"</span>.equals(property)) &#123;</span><br><span class="line">                                   <span class="keyword">int</span> index = value.lastIndexOf(<span class="string">"."</span>);</span><br><span class="line">                                   String throwRef = value.substring(<span class="number">0</span>, index);</span><br><span class="line">                                   String throwMethod = value.substring(index + <span class="number">1</span>);</span><br><span class="line">                                   reference = <span class="keyword">new</span> RuntimeBeanReference(throwRef);</span><br><span class="line">                                   beanDefinition.getPropertyValues().addPropertyValue(<span class="string">"onthrowMethod"</span>, throwMethod);</span><br><span class="line">                               &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"oninvoke"</span>.equals(property)) &#123;</span><br><span class="line">                                   <span class="keyword">int</span> index = value.lastIndexOf(<span class="string">"."</span>);</span><br><span class="line">                                   String invokeRef = value.substring(<span class="number">0</span>, index);</span><br><span class="line">                                   String invokeRefMethod = value.substring(index + <span class="number">1</span>);</span><br><span class="line">                                   reference = <span class="keyword">new</span> RuntimeBeanReference(invokeRef);</span><br><span class="line">                                   beanDefinition.getPropertyValues().addPropertyValue(<span class="string">"oninvokeMethod"</span>, invokeRefMethod);</span><br><span class="line">                               &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                                   <span class="keyword">if</span> (<span class="string">"ref"</span>.equals(property) &amp;&amp; parserContext.getRegistry().containsBeanDefinition(value)) &#123;</span><br><span class="line">                                       BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);</span><br><span class="line">                                       <span class="keyword">if</span> (!refBean.isSingleton()) &#123;</span><br><span class="line">                                           <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"The exported service ref "</span> + value + <span class="string">" must be singleton! Please set the "</span> + value + <span class="string">" bean scope to singleton, eg: &lt;bean id=\""</span> + value + <span class="string">"\" scope=\"singleton\" ...&gt;"</span>);</span><br><span class="line">                                       &#125;</span><br><span class="line">                                   &#125;</span><br><span class="line">                                   reference = <span class="keyword">new</span> RuntimeBeanReference(value);</span><br><span class="line">                               &#125;</span><br><span class="line">                               beanDefinition.getPropertyValues().addPropertyValue(propertyName, reference);</span><br><span class="line">                           &#125;</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       NamedNodeMap attributes = element.getAttributes();</span><br><span class="line">       <span class="keyword">int</span> len = attributes.getLength();</span><br><span class="line">       <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">           Node node = attributes.item(i);</span><br><span class="line">           String name = node.getLocalName();</span><br><span class="line">           <span class="keyword">if</span> (!props.contains(name)) &#123;</span><br><span class="line">               <span class="keyword">if</span> (parameters == <span class="keyword">null</span>) &#123;</span><br><span class="line">                   parameters = <span class="keyword">new</span> ManagedMap();</span><br><span class="line">               &#125;</span><br><span class="line">               String value = node.getNodeValue();</span><br><span class="line">               parameters.put(name, <span class="keyword">new</span> TypedStringValue(value, String.class));</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">if</span> (parameters != <span class="keyword">null</span>) &#123;</span><br><span class="line">           beanDefinition.getPropertyValues().addPropertyValue(<span class="string">"parameters"</span>, parameters);</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> beanDefinition;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>DubboBean的解析主要是依赖于Spring的生命周期和factoryBean来实现的。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/dubbo/" rel="tag"># dubbo</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/dubbo/export/" rel="next" title="服务导出">
                <i class="fa fa-chevron-left"></i> 服务导出
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/mysql/索引访问类型/" rel="prev" title="索引访问类型">
                索引访问类型 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/uploads/avatar.jpg" alt="arry">
            
              <p class="site-author-name" itemprop="name">arry</p>
              <p class="site-description motion-element" itemprop="description">Java源码/设计模式/个人记录</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">42</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#代码解析"><span class="nav-number">1.</span> <span class="nav-text">代码解析</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">arry</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
